package com.google.code.timetrail.tests;

import static org.junit.Assert.*;

import java.util.Random;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.google.code.timetrail.backend.*;
/**
 * This class contains the test for the following store methods- canBuy, buy, getTempWeightCost and getTempWeight
 * @author Jake Gibel
 *
 */
public class StoreTest {
    private Control control;
    private Person player;
    private Store store;
    Inventory inv;
    @Before
    public void setUp(){
        control = new Control();
    }
    
    @Test
    public void testBuy(){
        //for(int i = 0; i <= 100; i++){
            //for(int j = 0; j <= 100; j++){
                for(int k = 0; k <= 100; k++){
                    //for(int l = 0; l <= 100; l++){
                        for(int m = 10; m < 1000; m++){
                        int[] skills = {0,0,k,0};
                        control = new Control();
                        control.getPlayer().setSkills(skills);

                        control.getInv().addItem(m, control.getInv().getMoney());
                        store = new Store(control);
                        int money = control.getInv().getMoney().getCurrentAmount();
                        
                        int foodAmount = 0;
                        int hullPartsAmount = 0;
                        int enginesAmount = 0;
                        int timeSuitsAmount = 0;
                        int timeBulletBillsAmount = 0;
                        int fuelCellsAmount = 0;
                        int fCapacitorAmount = 0;
                        Random rand = new Random();
                        for(int count = 0; count < 10; count++){
                            Item item = control.getInv().getRandomItemNotMoney();
                            
                            int amount = rand.nextInt(10);

                                if(item instanceof Food){ 
                                    if(store.canBuyItem(amount, item)){
                                        store.buyItem(amount, item);
                                        money -= amount * (item.getValue() - ((item.getValue() * k) / 100));
                                        foodAmount += amount;
                                    }
                                    assertEquals(foodAmount, control.getInv().getFood().getCurrentAmount());
                                    //assertEquals(money, control.getInv().getMoney().getCurrentAmount());
                                } else if(item instanceof HullPart){
                                    if(store.canBuyItem(amount, item)){
                                        store.buyItem(amount, item);
                                        money -= amount * (item.getValue() - ((item.getValue() * k) / 100));

                                        hullPartsAmount += amount;
                                    }
                                    assertEquals(hullPartsAmount, control.getInv().getHullParts().getCurrentAmount());
                                    //assertEquals(money, control.getInv().getMoney().getCurrentAmount());
                                } else if(item instanceof Engine){
                                    if(store.canBuyItem(amount, item)){
                                        store.buyItem(amount, item);
                                        money -= amount * (item.getValue() - ((item.getValue() * k) / 100));

                                        enginesAmount += amount;
                                    }
                                    assertEquals(enginesAmount, control.getInv().getEngines().getCurrentAmount());
                                } else if(item instanceof TimeSuit){
                                    if(store.canBuyItem(amount, item)){
                                        store.buyItem(amount, item);
                                        money -= amount * (item.getValue() - ((item.getValue() * k) / 100));

                                        timeSuitsAmount += amount;
                                    }
                                    assertEquals(timeSuitsAmount, control.getInv().getTimeSuits().getCurrentAmount());
                                } else if(item instanceof TimeBulletBill){
                                    if(store.canBuyItem(amount, item)){
                                        store.buyItem(amount, item);
                                        money -= amount * (item.getValue() - ((item.getValue() * k) / 100));

                                        timeBulletBillsAmount += amount;
                                    }
                                    assertEquals(timeBulletBillsAmount, control.getInv().getTimeBulletBills().getCurrentAmount());
                                } else if(item instanceof FuelCell){
                                    if(store.canBuyItem(amount, item)){
                                        store.buyItem(amount, item);
                                        money -= amount * (item.getValue() - ((item.getValue() * k) / 100));

                                        fuelCellsAmount += amount;
                                    }
                                    assertEquals(fuelCellsAmount, control.getInv().getFuelCells().getCurrentAmount());
                                } else if(item instanceof FluxCapacitor){
                                    if(store.canBuyItem(amount, item)){
                                        store.buyItem(amount, item);
                                        money -= amount * (item.getValue() - ((item.getValue() * k) / 100));

                                        fCapacitorAmount += amount;
                                }
                                    assertEquals(fCapacitorAmount, control.getInv().getFCapacitors().getCurrentAmount());
                            }
                        }
                        }
                    }

    }
    
    @Test
    public void testCanBuy(){   
                for(int k = 0; k <= 100; k++){
                        int[] skills = {0,0,k,0};
                        control.getPlayer().setSkills(skills);
                        store = new Store(control);
                        int cantbuycount = 0;
                        for(int count = 0; count < 100; count++){
                            Item item = control.getInv().getRandomItemNotMoney();
                            Random rand = new Random();
                            int amount = rand.nextInt(10);                           
                            boolean canBuy = control.getInv().canAddItem(amount, item) 
                                    && control.getInv().canRemoveItem((amount) - ((amount * k)/100), control.getInv().getMoney());
                            assertEquals(store.canBuyItem(amount, item), canBuy);  
                            if(canBuy){
                                store.buyItem(amount, item);
                            } else {
                                if(cantbuycount >= 10){
                                    control.setInv(new Inventory());
                                    store = new Store(control);
                                    cantbuycount = 0;
                                } else {
                                    cantbuycount++;
                                }
                            }
                        }

                }

    }
    
    @Test
    public void testGetTempWeightCost(){

                for(int k = 0; k <= 100; k++){

                        int[] skills = {0,0,k,0};
                        control.getPlayer().setSkills(skills);
                        int weightTotal = 0;
                        int costInitial = control.getInv().getMoney().getCurrentAmount();
                        int costTotal = costInitial;
                        store = new Store(control);
                        int prevFoodAmount = 0;
                        int prevHullPartsAmount = 0;
                        int prevEnginesAmount = 0;
                        int prevTimeSuitsAmount = 0;
                        int prevTimeBulletBillsAmount = 0;
                        int prevFuelCellsAmount = 0;
                        int prevFCapacitorAmount = 0;
                        for(int count = 0; count < 10; count++){
                            
                            Item item = control.getInv().getRandomItemNotMoney();
                            Random rand = new Random();
                            int amount = rand.nextInt(1000);
                            
                            if(item instanceof Food){ 
                                store.setTempFood(amount);
                                weightTotal -= item.getWeight() * prevFoodAmount;
                                weightTotal += item.getWeight() * amount;
                                costTotal += prevFoodAmount * (item.getValue() - ((item.getValue() * k) / 100));
                                costTotal = costTotal - amount * (item.getValue() - ((item.getValue() * k) / 100));
                                prevFoodAmount = amount;
                                assertEquals(weightTotal, store.getTempWeight());
                                assertEquals(costTotal, store.getTempCost());
                            } else if(item instanceof HullPart){
                                store.setTempHullParts(amount);
                                weightTotal -= item.getWeight() * prevHullPartsAmount;
                                weightTotal += item.getWeight() * amount;
                                costTotal += prevHullPartsAmount * (item.getValue() - ((item.getValue() * k) / 100));
                                costTotal = costTotal - amount * (item.getValue() - ((item.getValue() * k) / 100));
                                prevHullPartsAmount = amount;
                                assertEquals(weightTotal, store.getTempWeight());
                                assertEquals(costTotal, store.getTempCost());
                            } else if(item instanceof Engine){
                                store.setTempEngines(amount);
                                weightTotal -= item.getWeight() * prevEnginesAmount;
                                weightTotal += item.getWeight() * amount;
                                costTotal += prevEnginesAmount * (item.getValue() - ((item.getValue() * k) / 100));
                                costTotal = costTotal - amount * (item.getValue() - ((item.getValue() * k) / 100));
                                prevEnginesAmount = amount;
                                assertEquals(weightTotal, store.getTempWeight());
                                assertEquals(costTotal, store.getTempCost());
                            } else if(item instanceof TimeSuit){
                                store.setTempTimeSuits(amount);
                                weightTotal -= item.getWeight() * prevTimeSuitsAmount;
                                weightTotal += item.getWeight() * amount;
                                costTotal += prevTimeSuitsAmount * (item.getValue() - ((item.getValue() * k) / 100));
                                costTotal = costTotal - amount * (item.getValue() - ((item.getValue() * k) / 100));
                                prevTimeSuitsAmount = amount;
                                assertEquals(weightTotal, store.getTempWeight());
                                assertEquals(costTotal, store.getTempCost());
                            } else if(item instanceof TimeBulletBill){
                                store.setTempTimeBulletBills(amount);
                                weightTotal -= item.getWeight() * prevTimeBulletBillsAmount;
                                weightTotal += item.getWeight() * amount;
                                costTotal += prevTimeBulletBillsAmount * (item.getValue() - ((item.getValue() * k) / 100));
                                costTotal = costTotal - amount * (item.getValue() - ((item.getValue() * k) / 100));
                                prevTimeBulletBillsAmount = amount;
                                assertEquals(weightTotal, store.getTempWeight());
                                assertEquals(costTotal, store.getTempCost());
                            } else if(item instanceof FuelCell){
                                store.setTempFuelCells(amount);
                                weightTotal -= item.getWeight() * prevFuelCellsAmount;
                                weightTotal += item.getWeight() * amount;
                                costTotal += prevFuelCellsAmount * (item.getValue() - ((item.getValue() * k) / 100));
                                costTotal = costTotal - amount * (item.getValue() - ((item.getValue() * k) / 100));
                                prevFuelCellsAmount = amount;
                                assertEquals(weightTotal, store.getTempWeight());
                                assertEquals(costTotal, store.getTempCost());
                            } else if(item instanceof FluxCapacitor){
                                store.setTempFCapacitors(amount);
                                weightTotal -= item.getWeight() * prevFCapacitorAmount;
                                weightTotal += item.getWeight() * amount;
                                costTotal += prevFCapacitorAmount * (item.getValue() - ((item.getValue() * k) / 100));
                                costTotal = costTotal - amount * (item.getValue() - ((item.getValue() * k) / 100));
                                prevFCapacitorAmount = amount;
                                assertEquals(weightTotal, store.getTempWeight());
                                assertEquals(costTotal, store.getTempCost());
                            }
                        }
                    }
                }
    
}
